#include "search.hpp"
#include "cpp-httplib/httplib.h"

const std::string root_path = "./wwwroot";
const std::string input = "./data/raw_html/raw.txt";

//void test(const httplib::Request &req, httplib::Response &res)

int main()
{
    httplib::Server svr;
    svr.set_base_dir(root_path.c_str());

    ns_search::search search;
    search.Initsearch(input);

    svr.Get("/s", [&search](const httplib::Request &req, httplib::Response &res) {
        if(!req.has_param("word"))
        {
            res.set_content("请输入你要搜索的关键字:", "text/plain; charset=utf-8");
            return;
        }
        std::string key_word = req.get_param_value("word");
        //std::cout << "搜索关键字:" << key_word << std::endl;
        lg(Info, "%s, %s, %s, %d", "搜索关键字:", key_word.c_str(), __FILE__, __LINE__);

        std::string jsonstr;
        search.Search(key_word, &jsonstr);
        res.set_content(jsonstr, "application/json");});
    
    svr.listen("0.0.0.0", 8081);
    
    return 0;
}